package com.atguigu.spzx.product.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.product.mapper.CategoryMapper;
import com.atguigu.spzx.product.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * ClassName: CategoryServiceImpl
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/5/22 9:24
 * Version 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private RedisTemplate<String,String> redisTemplate;
    //category::one
    //查询所有一级分类  树形封装
    // category::all
    @Cacheable(value = "caterogy",key = "'all'")
    @Override
    public List<Category> selectOneCategory() {
        //1 查询redis 看是否有全部一级分类
        String categoryOneJson = redisTemplate.opsForValue().get("category::one");

        //2 如果有所有的一级分类  直接返回
        if(StringUtils.isEmpty(categoryOneJson)){
            //字符串转换为list集合
            List<Category> existCategoryList = JSON.parseArray(categoryOneJson, Category.class);
            return existCategoryList;
        }

        //3 如果不包含所有一级分类  查询数据库  返回内容放入redis
        List<Category> categoryList = categoryMapper.selectOneCategory();
        redisTemplate.opsForValue().set("category::one",JSON.toJSONString(categoryList),
                7, TimeUnit.DAYS);

        return categoryList;
    }

    @Override
    public List<Category> findCategoryTree() {
        //1 查询所有分类  返回一个list集合
        List<Category> allCategoryList = categoryMapper.findAll();

        //2 遍历所有分类的集合通过parentId值=0得到所有一级分类
        List<Category> oneCategoryList =
                allCategoryList.stream().filter(item -> item.getParentId().longValue() == 0).collect(Collectors.toList());

        //3 遍历所有一级分类list集合   条件判断：id = parent_id对应关系得到一级下面的二级分类
        if(!CollectionUtils.isEmpty(oneCategoryList)) {
            oneCategoryList.forEach(oneCategory -> {
                List<Category> twoCategoryList = allCategoryList.stream().filter(item -> item.getParentId().longValue() == oneCategory.getId().longValue()).collect(Collectors.toList());
                oneCategory.setChildren(twoCategoryList);

                //4 遍历所有二级分类的集合得到三级分类  id = parent_id
                if(!CollectionUtils.isEmpty(twoCategoryList)) {
                    twoCategoryList.forEach(twoCategory -> {
                        List<Category> threeCategoryList = allCategoryList.stream().filter(item -> item.getParentId().longValue() == twoCategory.getId().longValue()).collect(Collectors.toList());
                        twoCategory.setChildren(threeCategoryList);
                    });
                }
            });
        }
        return oneCategoryList;
    }
}
